package com.ybwh.springboot2.service;

import com.google.common.base.Preconditions;
import com.ybwh.springboot2.common.BusinessException;
import com.ybwh.springboot2.dao.AreaMapper;
import com.ybwh.springboot2.model.po.Area;
import com.ybwh.springboot2.util.BatchActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 测试spring事务管理
 * </p>
 *
 * @author fanbeibei
 * @version V1.0
 * @since 2019/8/14 11:04
 */
@Service
public class AreaService {
    @Autowired
    private AreaService2 areaService2;
    @Resource
    private AreaMapper areaMapper;

    @Transactional(rollbackFor = Exception.class)
    public void addAreaRollBack0(Area area) throws BusinessException {
        Preconditions.checkArgument(null != area, "area is null");

        areaMapper.insert(area);
        throw new BusinessException("");

    }

    @Transactional(rollbackFor = Exception.class)
    public void addAreaRollBack1(Area area) throws BusinessException {
        Preconditions.checkArgument(null != area, "area is null");
        areaMapper.insert(area);
    }

    //    @Transactional(rollbackFor = Exception.class)
    public void addAreaRollBack2(Area area) throws BusinessException {
        Preconditions.checkArgument(null != area, "area is null");

        addAreaRollBack1(area);
        throw new BusinessException("");

    }


    public void addAreaRollBack3(Area area) throws BusinessException {
        Preconditions.checkArgument(null != area, "area is null");

        addAreaRollBack1(area);
        throw new BusinessException("");

    }


    public void addAreaRollBack4(Area area) throws BusinessException {
        Preconditions.checkArgument(null != area, "area is null");

        areaService2.addAreaRollBack1(area);
        throw new BusinessException("");

    }


    public int batchInsert(List<Area> areaList) {
        Preconditions.checkArgument(null != areaList && areaList.size() > 0, "areaList is null");

        return BatchActionUtil.batchInsertWrap(areaList, 200, new BatchActionUtil.BatchAction<Area, Object>() {
            @Override
            public int insert(List<Area> list) {
                return areaMapper.batchInsert(list);
            }
        });


    }

    public Area queryById(Long id) {

        return areaMapper.selectById(id);

    }
}
